#include <string>
#include <iostream>
#include <unistd.h>
#include <sys/time.h>
#include <unistd.h>
#include "socket.hpp"
#include <stdlib.h>

#define NUM sizeof(fd_set) * 8

void Usage(const std::string &proc)
{
    std::cout << "Usage: " << proc << " port" << std::endl;
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }
    int listen_sock = Sock::Socket();
    uint16_t Port = (uint16_t)atoi(argv[1]);
    Sock::Bind(listen_sock, Port);
    Sock::Listen(listen_sock);
    fd_set rfd;
    int fd_array[NUM];
    for (int i = 0; i < NUM; ++i)
    {
        fd_array[i] = -1;
    }
    fd_array[0] = listen_sock;
    for (;;)
    {
        FD_ZERO(&rfd);
        int max_fd = fd_array[0];
        for (int i = 0; i < NUM; ++i)
        {
            if (fd_array[i] == -1)
                continue;
            // 合法的fd
            FD_SET(fd_array[i], &rfd);
            if (max_fd < fd_array[i])
                max_fd = fd_array[i];
        }
        struct timeval timeout = {5, 0};
        int n = select(max_fd + 1, &rfd, nullptr, nullptr, nullptr);
        switch (n)
        {
        case -1:
            std::cout << "select error" << std::endl;
            break;
        case 0:
            std::cout << "timeout " << std::endl;
            break;
        default:
            for (int i = 0; i < NUM; ++i)
            {
                if (fd_array[i] == -1)
                    continue;
                // 合法的fd
                if (FD_ISSET(fd_array[i], &rfd))
                {
                    if (listen_sock == fd_array[i])
                    {
                        int sock = Sock::Accept(listen_sock);
                        if (sock >= 0)
                        {
                            int j = 1;
                            for (; j < NUM; ++j)
                            {
                                if (fd_array[j] == -1)
                                    break;
                            }
                            // 服务器已经满载了
                            if (j == NUM)
                            {
                                std::cout << "服务器已经满载了！" << std::endl;
                                close(sock);
                            }
                            else
                            {
                                fd_array[j] = sock;
                            }
                        }
                    }
                    else
                    {
                        // recv/read
                        char buffer[1024] = {0};
                        ssize_t s = read(fd_array[i], buffer, sizeof(buffer) - 1);
                        if (s > 0)
                        {

                            std::cout << "client[" << fd_array[i] << "]# " << buffer << std::endl;
                        }
                        // 关闭链接了
                        else if (s == 0)
                        {
                            close(fd_array[i]);
                            fd_array[i] = -1;
                            std::cout << fd_array[i] << " 已经关闭了链接" << std::endl;
                        }
                        // 读取失败了
                        else
                        {
                            close(fd_array[i]);
                            fd_array[i] = -1;
                            std::cout << "读取数据失败 " << std::endl;
                        }
                    }
                }
            }
            break;
        }
    }

    return 0;
}